import db from './createDb'
import Mock from 'mockjs'
import jwt from 'jsonwebtoken'
import md5 from 'md5'
import { parseUrl, pick } from '../assets/utils/toolFunction'

// 读取当前管理员信息
Mock.mock('user/info', 'get', () => {
  let jwtInfo = {}
  try {
    const token = localStorage.getItem('ZY-Token')
    jwtInfo = jwt.decode(token, '0401')
  } catch (err) {
    console.log(err)
  }
  const result = db.select('zy_users', item => item.userName === jwtInfo.data).map(item => {
    return pick(item, ['id', 'userName', 'nickName', 'avatar', 'mobile', 'state'])
  })
  return {
    err: 0,
    desc: '信息获取成功',
    result
  }
})

// 添加用户
Mock.mock('user/add', 'post', ({ body }) => {
  const { userName, password, nickName, mobile, avatar, state = 1 } = JSON.parse(body)

  const nameRepeat = db.select('zy_users', item => item.userName === userName)
  // 判断用户名是否存在
  if (nameRepeat && nameRepeat.length) {
    return {
      err: 1002,
      desc: '该账号已被注册,请重新输入账号'
    }
  }
  const mobileRepeat = db.select('zy_users', item => item.mobile === mobile)
  // 判断手机号是否被使用
  if (mobileRepeat && mobileRepeat.length) {
    return {
      err: 1004,
      desc: '手机号码已经被注册，请重新输入'
    }
  }
  db.add('zy_users', { userName, password: md5(password), nickName, mobile, avatar, state })
  return {
    err: 0,
    desc: '添加成功'
  }
})

// 读取用户列表信息
Mock.mock(/user\/list(\?.+)?/, 'get', ({ url }) => {
  let { state, keyword, page, pageSize } = parseUrl(url)
  // 转为数字
  state = +state
  page = +page
  pageSize = +pageSize
  // 处理中文乱码问题
  keyword = keyword ? decodeURIComponent(keyword) : keyword

  const searchResult = db.select('zy_users', item => {
    // 搜索条件及不是超级管理员
    return (
      item.userName.includes(keyword) ||
      item.nickName.includes(keyword) ||
      item.mobile.includes(keyword) || !keyword
    ) && (item.state === state || !state) && item.userName !== 'admin'
  })
  const total = searchResult.length
  const totalPage = Math.ceil(total / pageSize)
  if (page > totalPage) page = totalPage
  const start = (page - 1) * pageSize
  const end = start + pageSize

  const pageResult = searchResult.slice(start, end)
  return { err: 0, result: pageResult, page, total, totalPage, pageSize, desc: '查询成功' }
})

// 修改用户
Mock.mock('user/updata', 'post', ({ body }) => {
  const { id, userName, password, nickName, mobile, avatar, state } = JSON.parse(body)
  const result = db.select('zy_users', item => item.id === id)[0]
  const { password: _password, mobile: _mobile } = result
  const mobileRepeat = db.select('zy_users', item => item.mobile === mobile && item.mobile !== _mobile)
  // 判断手机号是否被使用
  if (mobileRepeat && mobileRepeat.length) {
    return {
      err: 1004,
      desc: '手机号码已经被注册，请重新输入'
    }
  }
  // 如果写了密码就修改,没写就用老密码
  const row = Object.assign({ userName, nickName, mobile, avatar, state }, { password: password ? md5(password) : _password })
  db.put('zy_users', id, row)
  return {
    err: 0,
    desc: '修改成功'
  }
})

// 修改state状态
Mock.mock('user/state', 'post', ({ body }) => {
  const { id, state } = JSON.parse(body)
  db.put('zy_users', id, { state })
  return {
    err: 0,
    desc: '状态修改成功'
  }
})

// 删除用户
Mock.mock('user/delete', 'post', ({ body }) => {
  const { ids } = JSON.parse(body)
  db.delete('zy_users', ids)
  return {
    err: 0,
    desc: '删除成功'
  }
})
